package model

import (
	"fmt"
	"high-one-hw1/appconfig"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB
var err error

func Init() {
	Config := appconfig.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Config.User,
		Config.Password,
		Config.Host,
		Config.Port,
		Config.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功", DB)
	}

	err = DB.AutoMigrate(&Shop{}, &User{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("迁移成功")
	}
}
